import http from '@/utils/http'

const BASE_URL = '/generator/deviceSituation'

/**
 * 通用响应结果接口
 */
interface Result<T = any> {
  code: string | number
  msg?: string
  message?: string
  data: T
  success: boolean
  timestamp: number
}

/**
 * 设备类别统计接口
 */
export interface DeviceCategoryStats {
  id?: string | null
  createUserString?: string | null
  createTime?: string | null
  category: number
  categoryName: string
  totalCount: number
  percentage: number
}

/**
 * 任务设备统计接口
 */
export interface TaskDeviceStats {
  category: number
  categoryName: string
  totalCount: number
}

/**
 * 按年获取设备类别统计数据
 */
export function listDeviceCategoryStats(year: number) {
  return http.get<Result<DeviceCategoryStats[]>>(`${BASE_URL}/categoryStats/${year}`)
}

/**
 * 按月获取设备类别统计数据
 */
export function listDeviceCategoryStatsByMonth(year: number, month: number) {
  return http.get<Result<DeviceCategoryStats[]>>(`${BASE_URL}/categoryStats/${year}/${month}`)
}

/**
 * 获取待归还设备统计（按年）
 */
export function listBorrowedDeviceStats(year: number) {
  return http.get<Result<DeviceCategoryStats[]>>(`${BASE_URL}/categoryStats/borrowed/${year}`)
}

/**
 * 获取待归还设备统计（按月）
 */
export function listBorrowedDeviceStatsByMonth(year: number, month: number) {
  return http.get<Result<DeviceCategoryStats[]>>(`${BASE_URL}/categoryStats/borrowed/${year}/${month}`)
}

/**
 * 获取即将过期设备统计（按年）
 */
export function listExpiringDeviceStats(year: number) {
  return http.get<Result<DeviceCategoryStats[]>>(`${BASE_URL}/categoryStats/expiring/${year}`)
}

/**
 * 获取即将过期设备统计（按月）
 */
export function listExpiringDeviceStatsByMonth(year: number, month: number) {
  return http.get<Result<DeviceCategoryStats[]>>(`${BASE_URL}/categoryStats/expiring/${year}/${month}`)
}

/**
 * 获取所有制造商列表
 */
export function listAllManufacturers() {
  return http.get<Result<string[]>>(`${BASE_URL}/manufacturers`)
}

/**
 * 根据制造商获取设备统计
 */
export function listManufacturerDeviceStats(manufacturer: string) {
  return http.get<Result<DeviceCategoryStats[]>>(
    `${BASE_URL}/categoryStats/byManufacturer/${encodeURIComponent(manufacturer)}`
  )
}

/**
 * 获取出库设备统计（按年）
 */
export function listOutboundDeviceStats(year: number) {
  return http.get<Result<DeviceCategoryStats[]>>(`${BASE_URL}/categoryStats/outbound/${year}`)
}

/**
 * 获取出库设备统计（按月）
 */
export function listOutboundDeviceStatsByMonth(year: number, month: number) {
  return http.get<Result<DeviceCategoryStats[]>>(`${BASE_URL}/categoryStats/outbound/${year}/${month}`)
}

/**
 * 根据任务ID获取设备统计
 */
export function listTaskDeviceStats(taskId: string) {
  return http.get<Result<TaskDeviceStats[]>>(`${BASE_URL}/deviceStats/byTask/${taskId}`)
}